﻿#include "mainwindow.h"
#include "qqml.h"

#include <QApplication>
#include <QQuickWidget>
#include <QCoreApplication>
#include <iostream>
#include <QLocale>
#include <QTranslator>
#include <QLocalServer>
#include <QLocalSocket>
#include <QSystemTrayIcon>
#include <QSGRendererInterface>
#include <QQuickWindow>

#include "info/videoinfo.h"
#include "info/videoinfolistmodel.h"
#include "global/mainglobalvar.h"
#include "app/snipasteapp.h"

bool isAppRunning()
{
    QLocalSocket socket;
    socket.connectToServer("com.huapi.huapi2d");

    if (socket.waitForConnected(500)) {
        // 通知已运行的实例显示警告
        socket.write("show_warning");
        socket.waitForBytesWritten();
        return true;
    }

    // 创建服务器以监听后续实例
    QLocalServer::removeServer("com.huapi.huapi2d");
    QLocalServer* server = new QLocalServer(qApp);
    QObject::connect(server, &QLocalServer::newConnection, [server]() {
        QLocalSocket* client = server->nextPendingConnection();
        QObject::connect(client, &QLocalSocket::readyRead, [client]() {
            QByteArray data = client->readAll();
            if (data == "show_warning") {
                // 在系统托盘显示通知
                QSystemTrayIcon* trayIcon = qobject_cast<QSystemTrayIcon*>(qApp->property("trayIcon").value<QObject*>());
                if (trayIcon) {
                    trayIcon->showMessage("提示", "程序已经在运行中，请查看系统托盘图标。", QSystemTrayIcon::Information, 3000);
                }
            }
            });
        });

    return !server->listen("com.huapi.huapi2d");
}

int main(int argc, char* argv[])
{
    // 防止QQuickWidget在Windows下使用OpenGL渲染时出现黑屏 https://forum.qt.io/topic/148089/qopenglwidget-doesn-t-work-with-qquickwidget/3
    if (0) {
        QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);
    }
    if (1) {
        QQuickWindow::setGraphicsApi(QSGRendererInterface::Software);
    }
    

    //QApplication::setAttribute(Qt::AA_UseSoftwareOpenGL);
    QApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.load(QUrl(":/QML/resources/QML/page.qml"));
    engine.load(QUrl(":/QML/resources/QML/VideoSearchResult.qml"));
    engine.load(QUrl(":/QML/resources/QML/SingleVideo.qml"));

    // 检查是否已有实例运行
    if (isAppRunning()) {
        return 0;
    }

    qmlRegisterType<VideoInfo>("QMLVideoInfo", 1, 0, "VideoInfo");
    qmlRegisterType<VideoInfoListModel>("QMLVideoInfoListModel", 1, 0, "VideoInfoListModel");

    // 设置本地化为 UTF-8
    std::locale::global(std::locale(""));

    // 加载翻译文件
    Language->setValue("");

    // 创建系统托盘图标
    QSystemTrayIcon* trayIcon = new QSystemTrayIcon(QIcon(":/software/favicon.ico"), &app);
    trayIcon->setToolTip("画皮的百宝箱");
    trayIcon->show();
    qApp->setProperty("trayIcon", QVariant::fromValue(trayIcon));

    MainWindow w;
    w.show();
    w.setupTrayIconMenu(trayIcon);

    // 启动 Snipaste 程序
    SnipasteApp snipasteApp(&w);
    QObject::connect(&w, &MainWindow::snapScreen, &snipasteApp, &SnipasteApp::snapScreen);

    return app.exec();
}